<?php

namespace App\Http\Controllers;

use App\Models\EarlyAlarm;
use Illuminate\Http\Request;

class EarlyAlarmController extends Controller
{
    public function index(Request $request)
    {
        $begin = $request->input('date_begin'); 
        $end = $request->input('date_end'); 
        if($begin){
            $data = EarlyAlarm::with(['operation','ESCluster' =>function($query){
                $query->with('ESUnit');
            }])->whereBetween('created_at', [$begin, $end])->requestCriteria($request)->paginate($request->query('limit'));
        }else{
            $data = EarlyAlarm::with(['operation','ESCluster' =>function($query){
                $query->with('ESUnit');
            }])->requestCriteria($request)->paginate($request->query('limit'));
        }
        return $this->respondSuccess($data);
    }

    public function show(EarlyAlarm $earlyAlarm)
    {
        $earlyAlarm->load(['ESCluster' =>function($query){
            $query->with('ESUnit');
        }]);
        // $earlyAlarm['e_s_cluster'] = $earlyAlarm->ESCluster()->with('ESUnit')->get();
        return $this->respondSuccess($earlyAlarm);
    }
}
